//
//  ImageManager.m
//  keyboard
//
//  Created by 遇水 on 16/3/25.
//  Copyright © 2016年 遇水. All rights reserved.
//

#import "ImageManager.h"
#import <Photos/Photos.h>
#import <AssetsLibrary/AssetsLibrary.h>

@implementation ImageManager


+(instancetype)sharedImageManager
{
    
    static ImageManager *manager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[ImageManager alloc]init];
    });
    return manager;
}


-(UIImage *)imageWithToPath:(NSString *)path
{
    __block UIImage *image;
    NSURL *imageUrl = [NSURL URLWithString:path];
    //通过路径在相册中获得图片
    if ([[[imageUrl scheme] lowercaseString] isEqualToString:@"assets-library"]) {
        //使用异步方式获取图片
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc]init];
            [assetsLibrary assetForURL:imageUrl resultBlock:^(ALAsset *asset) {
                ALAssetRepresentation *rep = [asset defaultRepresentation];
                CGImageRef imageRef = [rep fullScreenImage];
                if (imageRef) {
                    image = [UIImage imageWithCGImage:imageRef];
                }
            } failureBlock:^(NSError *error) {
                NSLog(@"图片获取失败");
            }];
        });
  
    }
    
    return image;
}
    
@end
